﻿Namespace AdvanceWars.Data
    ''' <summary>
    ''' 基础数据服务器
    ''' </summary>
    Public NotInheritable Class DataService

        Private Shared romData As RomData

        Shared Sub New()
            romData = New RomData
            WeaponFactory = Factory(Of WeaponType).LoadData("Weapons.xml")
            romData.Weapons = WeaponFactory
            'UnitFactory = Factory(Of UnitType).LoadData("Units.xml")
            LoadDamageChart()
        End Sub

#Region "DataService"

        ''' <summary>
        ''' 随机种子
        ''' </summary>
        Public Shared ReadOnly Random As New Random

        ''' <summary>
        ''' 单位工厂
        ''' </summary>
        Public Shared Property UnitFactory As Factory(Of UnitType)

        ''' <summary>
        ''' 武器工厂
        ''' </summary>
        Public Shared Property WeaponFactory As Factory(Of WeaponType)

        ''' <summary>
        ''' 地形工厂
        ''' </summary>
        Public Shared Property TerrainFactory As Factory(Of TerrainType)

        Public Shared Function GetUnit(ByVal id As Integer) As UnitType
            Return romData.Units(id)
        End Function

        Public Shared Function GetWeapon(ByVal id As Integer) As WeaponType
            Return romData.Weapons(id)
        End Function

        Public Shared Function GetTerrain(ByVal id As Integer) As TerrainType
            Return romData.Terrains(id)
        End Function

        ''' <summary>
        ''' 伤害表查询
        ''' </summary>
        Public Shared Function GetDamage(ByVal weaponId As Integer, ByVal unitId As Integer) As Double
            Return damageChart(weaponId, unitId)
        End Function

        ''' <summary>
        ''' 移动查询
        ''' </summary>
        Public Shared Function GetCost(ByVal unitId As Integer, ByVal terrainId As Integer) As Integer
            Return moveCostChart(unitId, terrainId)
        End Function

        ''' <summary>
        ''' WeaponId and UnitId chart
        ''' </summary>
        Private Shared damageChart(,) As Double

        ''' <summary>
        ''' MoveTypeId and TerrainId chart
        ''' </summary>
        Private Shared moveCostChart(,) As Integer

#End Region

        Private Shared Sub LoadDamageChart()
            Dim dt As DataTable = Csv.StringRead(My.Resources.DamageChart)
            ReDim damageChart(27, 25)
            For i = 0 To dt.Rows.Count - 1
                For j = 0 To dt.Columns.Count - 1
                    damageChart(i, j) = dt.Rows(i)(j)
                Next
            Next
        End Sub

        Private Shared Sub LoadMoveCostChart()
            ReDim moveCostChart(7, 12)
            Dim dt As DataTable = Csv.Read("123.csv")
            For i = 0 To dt.Rows.Count - 1
                For j = 0 To dt.Columns.Count - 1
                    moveCostChart(i, j) = dt.Rows(i)(j)
                Next
            Next
        End Sub

    End Class
End Namespace